﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Reflection;

namespace devtm.Editor.TypeSelector.AttributeBuilder
{
    /// <summary>
    /// Interaction logic for TitleChoosePropertyLastUserControl.xaml
    /// </summary>
    public partial class TitleChoosePropertyLastUserControl : UserControl, INotifyPropertyChanged
    {
        public TitleChoosePropertyLastUserControl()
        {
            InitializeComponent();
            DataContext = this;

        }

        private bool _isFirst;
        public bool IsFirst
        {
            get { return _isFirst; }
            set
            {
                _isFirst = value;
                if (value)
                    Img.Content = "{";
                else
                    Img.Content = "}";

            }
        }


        public Func<List<PropertyInfo>> AvailbleProperty { get; set; }

        public List<PropertyInfo> Properties { get { return AvailbleProperty(); } }

        public Visibility AddVisibility
        {
            get
            {

                if (_NotAction > DateTime.Now || MyComboList.IsDropDownOpen)
                    return System.Windows.Visibility.Collapsed;

                if (_isFirst || ComboFocus && Properties.Count() == 0)
                    return System.Windows.Visibility.Collapsed;

                if (Properties.Count() > 0)
                    return Visibility.Visible;

                return System.Windows.Visibility.Collapsed;

            }
        }

        public Visibility ComboVisibility
        {
            get
            {

                if (AddVisibility == System.Windows.Visibility.Collapsed && !_isFirst && Properties.Count() != 0)
                    return System.Windows.Visibility.Visible;

                return System.Windows.Visibility.Collapsed;

            }
        }

        #region INotifyPropertyChanged

        public void MyPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }

        public void MyPropertyChanged(string propertyName)
        {
            MyPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private DateTime _NotAction;
        internal void RefreshAdd()
        {
            MyPropertyChanged("AddVisibility");
            MyPropertyChanged("ComboVisibility");
            MyPropertyChanged("Properties");
        }

        bool ComboFocus;
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ComboFocus = true;
            _NotAction = DateTime.Now.AddMilliseconds(500);
            MyPropertyChanged("AddVisibility");
            MyPropertyChanged("ComboVisibility");
            MyPropertyChanged("Properties");

            MyComboList.Focus();
            
            Visibility l = MyComboList.Visibility;

            Lost(this);
        }

        private void MyComboList_LostFocus(object sender, RoutedEventArgs e)
        {

            if (_NotAction > DateTime.Now)
            {
                MyComboList.IsDropDownOpen = true;
                return;
            }

            if (MyComboList.IsDropDownOpen)
                return;

            ComboFocus = false;

            MyPropertyChanged("AddVisibility");
            MyPropertyChanged("ComboVisibility");

            Lost(this);
        }

        private void MyComboList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AddProperty((PropertyInfo)MyComboList.SelectedItem);

            try
            {
                MyPropertyChanged("Properties");
            }
            catch (Exception)
            {

            }

            ComboFocus = false;

            MyPropertyChanged("AddVisibility");
            MyPropertyChanged("ComboVisibility");

            Lost(this);

        }

        public Action<PropertyInfo> AddProperty;

        private void MyComboList_DropDownClosed(object sender, EventArgs e)
        {

            ComboFocus = false;

            MyPropertyChanged("AddVisibility");
            MyPropertyChanged("ComboVisibility");
            Lost(this);

        }

        public Action<object> Lost { get; set; }

    }
}
